//
//  ViewController.m
//  CarsBrand
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *cars;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
    self.cars = [NSArray arrayWithContentsOfFile:path];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cars.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr=[[NSArray alloc]init];
    arr=self.cars[section][@"cars"];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSArray *arr=[[NSArray alloc]init];
    arr=self.cars[indexPath.section][@"cars"];
    cell.textLabel.text = arr[indexPath.row][@"name"];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *tmp = [[NSString alloc] init];
    tmp = self.cars[section][@"title"];
    return tmp;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    for(int i=0;i<self.cars.count;i++)
    {
        NSString *str=[NSString stringWithFormat:@"%@",self.cars[i][@"title"]];
        [arr addObject:str];
    }
    return arr;
}
@end
